/*
 * (c) ralfoide, http://gamez.googlecode.com/, 2008
 * Project: gamez
 * License TBD
 */


package com.alfray.gamez.gameplay;

import javax.microedition.khronos.opengles.GL10;

import android.view.KeyEvent;
import android.view.View;

import com.alfray.gamez.R;
import com.alfray.gamez.GamezActivity.Context;
import com.alfray.gamez.GamezActivity.GameplayId;
import com.alfray.gamez.shape.Flag;
import com.alfray.gamez.shape.Shape;

//-----------------------------------------------

public class Title extends Gameplay {

    private static final int NUM_CELL_WIDTH = 8;
    private static final int NUM_CELL_HEIGHT = 8;
    private static final int FPS_MS = 1000/25; // 15 FPS

	private Flag mSprite;

	private static final int SIN_TABLE_SIZE = 256; // must be a power of 2
	private static final int SIN_TABLE_MASK = SIN_TABLE_SIZE - 1;
	private static int[] mSinTable = new int[SIN_TABLE_SIZE];
	
	static {
		final double coef = Math.PI / SIN_TABLE_SIZE;
		for (int i = 0; i < SIN_TABLE_SIZE; i++) {
			mSinTable[i] = (int)(65536.0 * Math.sin(i * coef)); 
		}
	}
	
    public Title(Context context) {
    	super(context);
    	setUseLighting(false);
    	setRenderSprites(true);
    	setUseTexture(true);
    	mSprite = new Flag(10, Shape.Color.BLUE, false /* checkerboard */);
	}

    @Override
	public String getMessage() {
		return "Click to continue";
	}

    @Override
    public void setupScene(GL10 gl, int w, int h) {
    	super.setupScene(gl, w, h);
    	// tell sprite to use lighting depending on current scene attributes
    	mSprite.setUseLighting(mUseLighting);
    	mSprite.setEnable(mRenderSprites);

    	gl.glDisable(GL10.GL_CULL_FACE);

    	mSprite.setUseTextureUnit(1);
    	associateTexture(gl, GL10.GL_TEXTURE0, 1);
    	loadTexture(gl, R.raw.title, GL10.GL_RGBA, 1);
    }

    /**
     * Draws one frame on the GL animation.
     * 
     * Called for every frame.
     */
    @Override
    public void drawScene(GL10 gl, int w, int h) {
    	super.drawScene(gl, w, h);

        //final int cw = NUM_CELL_WIDTH;
        //final int ch = NUM_CELL_HEIGHT;
        final int cw16 = NUM_CELL_WIDTH << 16;
        final int ch16 = NUM_CELL_HEIGHT << 16;

        long ms_since_start = System.currentTimeMillis() - mContext.mStartTime;
        int a = (int) (ms_since_start >> 5);
        a = mSinTable[a & SIN_TABLE_MASK];
        int rot = (a << 6) - (1 << (16+6-1)); 
        gl.glRotatex(rot, 0, 0, 1 << 16); // rot on Z, in degrees

        a = (int) (ms_since_start >> 4);
        a = mSinTable[a & SIN_TABLE_MASK];
        rot = (a << 7) - (1 << (16+7-1)); 
        gl.glRotatex(rot, 1 << 16, 0, 0); // rot on X in degrees

        gl.glScalex(cw16, ch16, 1 << 16);
		mSprite.drawTo(gl);
    }
    
    @Override
    public int threadUpdate(Context context) {
    	long start_ms = System.currentTimeMillis();
        long ms_since_start = start_ms - context.mStartTime;
        mSprite.animate(ms_since_start);
    	long time_here_ms = System.currentTimeMillis() - start_ms;
    	mContext.mGameplayThreadMs = time_here_ms;
        return (int) (FPS_MS - time_here_ms); // sleep 100 ms
    }

	public void onClick(View view) {
		// switch to the game itself
		mContext.switchGameplay(GameplayId.Shooter);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SPACE ||
				keyCode == KeyEvent.KEYCODE_NEWLINE) {
			return mContext.switchGameplay(GameplayId.Shooter); 
		}
		return super.onKeyUp(keyCode, event);
	}
}


